<?php
class TagsController extends AppController {

	var $name = 'Tags';
	var $components = array('Site');

  function index(){
    $tagsData = $this->Tag->query("SELECT tags. * , count( posts_tags.tag_id ) postCount
                            FROM tags tags
                            LEFT JOIN posts_tags posts_tags ON tags.id = posts_tags.tag_id
                            GROUP BY tags.id
                            ORDER BY tags.tag");
     $this->set('tags', $tagsData);

    $postCounts = array();

   if (is_array($tagsData) && count($tagsData) > 0) {
     foreach ($tagsData as $tagDetails) {
        $postCounts[] =  $tagDetails[0]['postCount'];
     }
   }
   else {
   	 $postCounts[] = 0;
   }
    $maxQuantity = max($postCounts);
    $minQuantity = min($postCounts);

    $spread = ($maxQuantity - $minQuantity);

    if ($spread == 0) {
        $spread = 1;
    }
		$step = ((30 - 10) / $spread);

		$this->set('step', $step);
    $this->set('spread', $spread);
    $this->set('minQuantity', $minQuantity);
   }

	function view($tag = null) {
		$tag=$this->Tag->find("Tag.tag='".$tag."'",null,null,-1);
    if(!$tag) {
			$this->Session->setFlash('There is no such tag.','msgerror');
			$this->redirect('/tags/');
			exit;
		}
		list($sort,$order)=$this->Site->getSort('modified');
    $count=$this->Tag->findCountByTag($tag['Tag']['id']);
		$fields="User.login, User.name, Category.name, Category.description, Post.id, Post.title, Post.replies, Post.closed, Post.created, Post.modified,Post.points,Post.rated";
		$posts=$this->Tag->findPostsByTag($tag['Tag']['id'],$fields,$sort." ".$order,$this->pager($count,PAGESIZE));
		$this->set('tag', $tag);
		$this->set('posts', $posts);
		$this->set('count', $count);
	}

	function admin_index() {
		$this->checkAdmin(); $this->layout='admin';
		$this->Tag->recursive = 0;
    $count=$this->Tag->findCount();
		$this->set('count',$count);
		$offset=($this->pager($count,APAGESIZE)*APAGESIZE)-APAGESIZE;
    $tagsData = $this->Tag->query("SELECT tags. * , count( posts_tags.tag_id ) postCount
                            FROM tags tags
                            LEFT JOIN posts_tags posts_tags ON tags.id = posts_tags.tag_id
                            GROUP BY tags.id
                            ORDER BY tags.id DESC LIMIT ".$offset.", ".APAGESIZE);
		//$this->set('tags', $this->Tag->findAll(null,null,"Tag.id DESC",APAGESIZE,$page,0));
		$this->set('tags', $tagsData);
	}

	function admin_edit($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(empty($this->data)) {
			if(!$id) {
				$this->redirect('/admin/tags/');
				exit;
			}
			$this->Tag->recursive=0;
			$this->data = $this->Tag->read(null, $id);
		} else {
			$this->cleanUpFields();
			if($this->Tag->save($this->data)) {
				$this->Session->setFlash('Tag modified.');
				$this->redirect('/admin/tags/');
			} else {
				$this->Session->setFlash('Fix errors.',"msgerror");
			}
		}
	}

	function admin_delete($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(!$id) {
			$this->redirect('/admin/tags/');
			exit;
		}
		if($this->Tag->del($id)) {
			$this->Session->setFlash('Tag deleted: id '.$id.'');
			$this->redirect('/admin/tags/');
		}
	}

}
?>